package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.shop.ShopInvoiceHeadSaveBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.ztgf.shopping.models.service.shop.IShopInvoiceHeadService;
import com.ztgf.shopping.models.bo.shop.ShopInvoiceHeadBO;
import com.ztgf.shopping.models.dto.shop.ShopInvoiceHeadDTO;

/**
 * 发票抬头管理表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-27 15:19:55
 */

@RestController
@RequestMapping("/api/shopInvoiceHead")
@Slf4j
@Api(value = "ShopInvoiceHeadApiController",tags = "发票抬头管理表Api接口")
public class ShopInvoiceHeadApiController {

    @DubboReference
    private IShopInvoiceHeadService shopInvoiceHeadService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopInvoiceHeadDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopInvoiceHeadDTO dto = shopInvoiceHeadService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "添加发票抬头管理表",notes = "添加发票抬头管理表")
    @PostMapping(value = "/insert")
    public Result insert( ShopInvoiceHeadSaveBO saveBO) {
        boolean flag = shopInvoiceHeadService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存发票抬头管理表",notes = "保存发票抬头管理表")
    public Result save( ShopInvoiceHeadSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改发票抬头管理表",notes = "修改发票抬头管理表")
    @PostMapping("/update")
    public Result update( ShopInvoiceHeadSaveBO saveBO){
        ShopInvoiceHeadDTO dto = new ShopInvoiceHeadDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = shopInvoiceHeadService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateDefault")
    @ApiOperation(value = "更新默认账户",notes = "更新默认账户")
    public Result updateDefault(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        boolean flag = shopInvoiceHeadService.updateDefault(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "发票抬头管理表列表",notes = "发票抬头管理表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopInvoiceHeadDTO>> queryPageList( ShopInvoiceHeadBO bo){
        Wrapper<ShopInvoiceHeadDTO> wrapper = new EntityWrapper<>();
        if (bo.getShopId() != null){
            wrapper.eq("shop_id", bo.getShopId());
        }

        wrapper.orderBy("create_time", false);//降序

        Page<ShopInvoiceHeadDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shopInvoiceHeadService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
